﻿Ext.define('app.prm.detail.DiscussDetail', {
    extend: 'Ext.panel.Panel',
    border: true,
    collapsed: true,
    collapsible: true,
    autoScroll: true,
    bodyStyle: 'padding:5px;',
    style: 'margin:3px',
    htmlContent: '',
    fileArray: [],
    topicId: 0,
    initComponent: function () {
        var me = this;
        me.items = [
            me.getTopicPanel(),
            me.getReplyPanel(),
            me.getReplyTextarea(),
            me.getUploadPanel()
        ];

        me.bbar = [
            "->",
            {
                text: '提交',
                icon: '/Content/images/accept.png',
                handler: function () {
                    var content = me.getReplyTextarea().getValue();
                    if (content == "") {
                        Ext.Msg.alert('系统提示', '请输入内容');
                        return false;
                    }
                    var file = me.getUploadPanel().getValue();
                    App.AjaxPost('/api/services/app/topic/CreateTopicReply', { TopicId: me.topicId, Content: content, UploadFile: file }, function () {
                        me.textReset();
                        me.initReply(me);
                    });
                }
            },
            {
                text: '重置',
                icon: '/Content/images/comment.png',
                handler: function () {
                    me.textReset();
                }
            }
        ];
        me.callParent();
    },
    textReset: function () {
        var me = this;
        me.getReplyTextarea().reset();
        me.getUploadPanel().removeAllArry();
    },
    listeners: {
        afterrender: function (me) {
            me.initReply(me);
            for (var i = 0; i < me.fileArray.length; i++) {
                var record = me.fileArray[i];
                var panel = new Ext.panel.Panel(
                {
                    border: false,
                    layout: 'table',
                    style: 'margin:0px 5px 0px 0px',
                    items: [
                        { xtype: 'displayfield', style: 'margin:0px 5px 0px 5px', value: record.name + '  <a style="color:red;">' + record.size + " Kb" + "</a>" },
                        { xtype: 'displayfield', style: 'margin:0px 5px 0px 5px', value: '<a href="/api/services/app/document/GetFile?path=' + record.path + '" style="color:blue;text-decoration:underline;cursor:pointer">下载</a>' }
                    ]
                });
                me.getTopicPanel().add(panel);
            }
        }
    },
    getTopicPanel: function () {
        var me = this;
        if (!me.topicPanel) {
            me.topicPanel = new Ext.panel.Panel({
                border: false,
                items: [
                    { border: false, html: '<b>' + me.htmlContent + '</b>' }
                ]
            });
        }
        return me.topicPanel;
    },
    getReplyTextarea: function () {
        if (!this.replyTextarea) {
            this.replyTextarea = new Ext.form.field.TextArea({
                emptyText: '回答',
                height: 150,
                width: '100%'
            });
        }
        return this.replyTextarea;
    },
    getUploadPanel: function () {
        var me = this;
        if (!me.uploadFile) {
            me.uploadFile = Ext.create('app.ux.UploadPanel');
        }
        return me.uploadFile;
    },
    initReply: function (me) {
        App.AjaxGet('/api/services/app/topic/GetTopicReply?topicId=' + me.topicId, function (data) {
            me.getReplyPanel().removeAll(true);
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];

                var panel = Ext.create('Ext.panel.Panel', {
                    style: 'margin:10px 0 10px 0',
                    border: false,
                    items: [
                        { border: false, style: 'color:blue', html: '<b>---' + record.createBy + '.' + record.createOn + '</b>' },
                        { border: false, style: 'margin:0 0 0 10px', html: record.content }
                    ]
                });
                me.getReplyPanel().add(panel);

                for (var j = 0; j < record.uploadFile.length; j++) {
                    var _record = record.uploadFile[j];
                    var _panel = new Ext.panel.Panel(
                    {
                        border: false,
                        layout: 'table',
                        style: 'margin:0px 5px 0px 0px',
                        items: [
                            { xtype: 'displayfield', style: 'margin:0px 5px 0px 5px', value: _record.name + '  <a style="color:red;">' + _record.size + " Kb" + "</a>" },
                            { xtype: 'displayfield', style: 'margin:0px 5px 0px 5px', value: '<a href="/api/services/app/document/GetFile?path=' + _record.path + '" style="color:blue;text-decoration:underline;cursor:pointer">下载</a>' }
                        ]
                    });
                    me.getReplyPanel().add(_panel);
                }
            }
        });
    },
    getReplyPanel: function () {
        if (!this.replyPanel) {
            this.replyPanel = new Ext.panel.Panel({
                border: false,
                items: []
            });
        }
        return this.replyPanel;
    }
})